// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SafFeatureReceiver.cs" company="Collaboris Ltd.">
//   Copyright (c) Collaboris Ltd. All rights Reserved.
// </copyright>
// <summary>
//   Defines the FeatureReceiverBase type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Collaboris.Saf.Adapters
{
    using Collaboris.Saf.Engine;
    using Collaboris.Saf.Tracing;
    using Collaboris.Utils;

    using Microsoft.SharePoint;

    /// <summary>
    /// Allows SAF to be called from a Feature. This cant be directly
    /// derived from <see cref="AdapterBase"/> as it has to derive from SPFeatureReceiver.
    /// Hence, the grunt of the work is done by <see cref="FeatureAdapter"/>.
    /// </summary>
    /// <remarks>
    /// Please Note! This class doesn't inherit from AdapterBase
    /// </remarks>
    public sealed class SafFeatureReceiver : SPFeatureReceiver
    {
        #region Constants and Fields

        /// <summary>
        /// The trace.
        /// </summary>
        private TraceHelper trace;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="SafFeatureReceiver"/> class. 
        /// Default Contstructor
        /// </summary>
        public SafFeatureReceiver()
        {
            this.trace = new AdaptersTraceHelper(this);
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Feature Activated Event
        /// </summary>
        /// <param name="properties">
        /// An <see cref="T:Microsoft.SharePoint.SPFeatureReceiverProperties"></see> 
        /// object that represents the properties of the event.
        /// </param>
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            this.trace.TraceMethodStart("FeatureActivated");

            var adapter = new FeatureAdapter(properties);

            adapter.ExecuteMacro(FeatureEvent.Activated);

            this.trace.TraceMethodEnd("FeatureActivated");
        }

        /// <summary>
        /// Feature Activated Event
        /// </summary>
        /// <param name="properties">
        /// An <see cref="T:Microsoft.SharePoint.SPFeatureReceiverProperties"></see> 
        /// object that represents the properties of the event.
        /// </param>
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            this.trace.TraceMethodStart("FeatureDeactivating");

            var adapter = new FeatureAdapter(properties);

            adapter.ExecuteMacro(FeatureEvent.Deactivating);

            this.trace.TraceMethodEnd("FeatureDeactivating");
        }

        /// <summary>
        /// Feature Activated Event
        /// </summary>
        /// <param name="properties">
        /// An <see cref="T:Microsoft.SharePoint.SPFeatureReceiverProperties"></see> 
        /// object that represents the properties of the event.
        /// </param>
        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
            this.trace.TraceMethodStart("FeatureInstalled");

            // do nothing
            this.trace.TraceMethodEnd("FeatureInstalled");
        }

        /// <summary>
        /// Feature Activated Event
        /// </summary>
        /// <param name="properties">
        /// An <see cref="T:Microsoft.SharePoint.SPFeatureReceiverProperties"></see> 
        /// object that represents the properties of the event.
        /// </param>
        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
            this.trace.TraceMethodStart("FeatureUninstalling");

            // do nothing
            this.trace.TraceMethodEnd("FeatureUninstalling");
        }

        #endregion

        #region Helper Methods

        /// <summary>
        /// Creates the Internal Feature Adapter
        /// </summary>
        /// <param name="properties">
        /// The properties.
        /// </param>
        /// <returns>
        /// </returns>
        private static FeatureAdapter GetAdapter(SPFeatureReceiverProperties properties)
        {

            var adapter = new FeatureAdapter(properties);


            return adapter;
        }
        #endregion
    }
}